import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld'
import login from '@/components/login'
import home from '@/components/home'
Vue.use(Router)

// export default new Router({
  const routes=[
    {
      path: '/',
      redirect:"/login"
    },
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/home',
      name: 'home',
      component: home,
      redirect:"/welcome",
      children:[
        {
          path:"/welcome",
          // component:welcome,
          component: resolve => require(['../components/pages/Welcome'], resolve),
        },
        {
          path:"/users",
          component: resolve => require(['../components/pages/user/User'], resolve),
        },
        {
          path:"/roles",
          component: resolve => require(['../components/pages/role/roles'], resolve),
        },
        {
          path:"/rights",
          component: resolve => require(['../components/pages/role/rights'], resolve),
        },
        {
          path:"/goods",
          component: resolve => require(['../components/pages/goods/goods'], resolve),
        },
        {
          path:"/addgoods",
          name:"addGoods",
          component: resolve => require(['../components/pages/goods/addgoods'], resolve),
        },
        {
          path:"/params",
          component: resolve => require(['../components/pages/goods/params'], resolve),
        },
        {
          path:"/categories",
          component: resolve => require(['../components/pages/goods/categories'], resolve),
        },
        {
          path:"/orders",
          component: resolve => require(['../components/pages/orders/orders'], resolve),
        },
        {
          path:"/reports",
          component: resolve => require(['../components/pages/reports/reports'], resolve),
        },
      ]
    },
  ]
// })

const router = new Router({
  routes
})

router.beforeEach((to,from,next) => {
  if(to.path==='/login') return next()
  // 获取token
  const token = window.sessionStorage.getItem('token')
  if(!token) return next('/login')
  next()
})

export default router
